package com.polarrose.spring.remoting.rest.support;

import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.BeansException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.polarrose.spring.remoting.rest.annotations.RestService;

public class RestAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware
{
    public static final Log log = LogFactory.getLog(RestAnnotationBeanPostProcessor.class);

    private ListableBeanFactory beanFactory;

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException
    {
        if (!(beanFactory instanceof ListableBeanFactory)) {
                throw new IllegalArgumentException("ListableBeanFactory required for EntityManagerFactory lookup");
        }

        this.beanFactory = (ListableBeanFactory) beanFactory;
    }


    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
    {
        RestService restServiceAnnotation = bean.getClass().getAnnotation(RestService.class);
        if (restServiceAnnotation != null)
        {
            log.debug("Processing " + bean.getClass().getName());
        }

        return true;
    }
}
